package com.godyao.mall.user.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.godyao.mall.user.boot.repository.SysPermissionRepository;
import com.godyao.mall.user.boot.repository.SysRoleRepository;
import com.godyao.mall.user.boot.service.ISysPermissionService;
import com.godyao.mall.user.common.model.SysPermission;
import com.godyao.mall.user.common.model.SysRole;
import com.godyao.mall.user.common.model.vo.SysRolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author godyao
 * @date 2022/4/2
 */
@Service
public class SysPermissionServiceImpl implements ISysPermissionService {
    @Autowired
    private SysPermissionRepository sysPermissionRepository;

    @Autowired
    private SysRoleRepository sysRoleRepository;

    @Override
    public List<SysRolePermissionVo> listRolePermission() {
        // 获取所有的权限集合
        final List<SysPermission> allPermissions = sysPermissionRepository.findAll();
        // 获取所有权限对应的角色
        final List<SysRolePermissionVo> rolePermissionVos = new ArrayList<>();
        allPermissions.parallelStream().peek(sysPermission -> {
            final List<SysRole> sysRoles = sysRoleRepository.listByPermissionId(sysPermission.getId());
            final SysRolePermissionVo sysRolePermissionVo = new SysRolePermissionVo();
            BeanUtil.copyProperties(sysPermission, sysRolePermissionVo);
            sysRolePermissionVo.setRoles(sysRoles);
            rolePermissionVos.add(sysRolePermissionVo);
        }).collect(Collectors.toList());
        return rolePermissionVos;
    }
}
